package com.lgkj.ui.userinfo;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.TeamAdapter;
import com.lgkj.bean.meitao.TeamBean;
import com.lgkj.ui.BaseActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/20 0020.
 * 我的团队
 */

public class TeamActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.tv_item_text)
    TextView tvItemText;
    @BindView(R.id.tv_item_count)
    TextView tvItemCount;
    @BindView(R.id.lv_item)
    ListView lvItem;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    @BindView(R.id.rb_item1)
    RadioButton rbItem1;
    @BindView(R.id.rb_item2)
    RadioButton rbItem2;
    @BindView(R.id.rb_item3)
    RadioButton rbItem3;
    private TeamAdapter adapter;
    private TeamBean teamBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_team;
    }

    @Override
    public void initView() {
        adapter = new TeamAdapter(TeamActivity.this);
        lvItem.setAdapter(adapter);
        lvItem.setOnItemClickListener(this);
    }

    @Override
    public void initData() {

        String level = SharedPreferencesUtils.getParam(TeamActivity.this, "level", "").toString();
        if (level.equals("3")) {
            rbItem2.setVisibility(View.VISIBLE);
            rbItem2.setChecked(true);
            getData("2");
        } else if (level.equals("4")) {
            rbItem1.setVisibility(View.VISIBLE);
            rbItem1.setChecked(true);
            rbItem2.setVisibility(View.VISIBLE);
            getData("3");
        }else {
            getData("1");
            rbItem3.setChecked(true);
        }
    }

    @OnClick({R.id.iv_back, R.id.rb_item1, R.id.rb_item2, R.id.rb_item3})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rb_item1:
                tvItemText.setText("美点合伙人");
                getData("3");
                break;
            case R.id.rb_item2:
                tvItemText.setText("美点创客");
                getData("2");
                break;
            case R.id.rb_item3:
                tvItemText.setText("美点会员");
                getData("1");
                break;
        }
    }

    // 0 普通用户 1会员 2 创客 3 合伙人 4 总代
    private void getData(String level) {
        CommonUtils.showDiaLog(TeamActivity.this, "正在查询中");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        map.put("level", level);
        HttpUtils.postHttp(map, CommonConstant.myTeam, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    int count = jsonObject.optJSONObject("data").optInt("total_count");
                    if (count > 0) {
                        teamBean = mGson.fromJson(String.valueOf(jsonObject), TeamBean.class);
                        if (teamBean.getData().getTotal_count() > 0) {
                            adapter.setDataBeans(teamBean.getData().getList().getData());
                        } else {
                            adapter.cclearData();
                        }
                        adapter.notifyDataSetChanged();
                        tvCount.setText(teamBean.getData().getTotal_count() + "");
                        tvItemCount.setText(teamBean.getData().getList().getTotal() + "");
                    }

                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String uid = adapter.getItem(position).getUser_info().getId();
        Intent intent = new Intent(TeamActivity.this, UserInfoByIdActivity.class);
        intent.putExtra("user_id", uid);
        intent.putExtra("user_level", adapter.getItem(position).getLevel());
        startsActivity(intent);
    }
}
